package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.ReservationMapper;
import com.hqyj.dao.TablesMapper;
import com.hqyj.pojo.Tables;
import com.hqyj.service.TablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 桌位表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-22
 */
@Service
public class TablesServiceImpl implements TablesService {

    @Autowired
    TablesMapper tablesMapper;
    @Autowired
    ReservationMapper reservationMapper;

    @Override
    public HashMap<String, Object> select(Tables tables) {
        HashMap<String, Object> map = new HashMap<>();
        //定义分页对象
        Page<Tables> page = new Page<>(tables.getPage(),tables.getRow());
        //条件构造器
        QueryWrapper<Tables> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(tables.getTableNum()) && tables.getTableNum() != null, "table_num", tables.getTableNum());

        //查询数据库
        Page<Tables> taList = tablesMapper.selectPage(page, queryWrapper);
        //当前页集合
        map.put("list", taList.getRecords());
        //总页数
        map.put("totalPage", taList.getPages());
        //总条数
        map.put("total", taList.getTotal());
        //当前页
        map.put("curPage", tables.getPage());
        if (tables.getPage() == 1) {
            //上一页
            map.put("prePage", 1);
        } else {
            map.put("prePage", tables.getPage() - 1);
        }
        if (tables.getPage() == taList.getPages()) {
            //下一页
            map.put("nextPage", taList.getPages());
        } else {
            map.put("nextPage", tables.getPage() + 1);
        }
        //每页显示条数
        map.put("row", tables.getRow());
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        int num = tablesMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> add(Tables tables) {
        HashMap<String, Object> map = new HashMap<>();

        //创建条件构造器
        QueryWrapper<Tables> queryWrapper = new QueryWrapper<>();
        //定义根据用户名查询的条件
        queryWrapper.eq("table_Num", tables.getTableNum());
        //查询用户名是否重名
        List<Tables> tablesList = tablesMapper.selectList(queryWrapper);
        if (tablesList.size() > 0) {
            map.put("info", "有重复桌号");
        } else {
            //新增
            int n = tablesMapper.insert(tables);
            if (n > 0) {
                map.put("info", "保存成功");
            } else {
                map.put("info", "保存失败");
            }
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Tables tables) {
        HashMap<String, Object> map = new HashMap<>();
        int num = tablesMapper.updateById(tables);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> tList(Tables tables) {
        HashMap<String, Object> map = new HashMap<>();
        //定义分页对象
//        Page<Tables> page = new Page<>(tables.getPage(),tables.getRow());
        Page<Tables> page = new Page<>();//全显示
        //条件构造器
        QueryWrapper<Tables> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(tables.getTableNum()) && tables.getTableNum() != null, "table_num", tables.getTableNum());

        //查询数据库
        Page<Tables> taList = tablesMapper.selectPage(page, queryWrapper);
        //当前页集合
        map.put("tablelist", taList.getRecords());
        //总页数
        map.put("totalPage", taList.getPages());
        //总条数
        map.put("total", taList.getTotal());
        //当前页
        map.put("curPage", tables.getPage());
        if (tables.getPage() == 1) {
            //上一页
            map.put("prePage", 1);
        } else {
            map.put("prePage", tables.getPage() - 1);
        }
        if (tables.getPage() == taList.getPages()) {
            //下一页
            map.put("nextPage", taList.getPages());
        } else {
            map.put("nextPage", tables.getPage() + 1);
        }
        //每页显示条数
        map.put("row", tables.getRow());
        return map;
    }
}
